package chpt04;

import java.util.Random;
import java.util.concurrent.atomic.AtomicLong;

class MyRandom extends Random {
	@Override
	protected int next(int bits) {
		// 仅仅是简单的复用父类的next(bits)方法
		return super.next(bits); 
	}

}

public class RandomTest {

	public static void main(String[] args) {
		// 基本使用
		final int NUM = 3;
		Random rnd = new Random();
		for (int i = 0; i < NUM; i++) {
			// 返回任意int值
			System.out.print(rnd.nextInt() + " ");
		}
		System.out.println();
		final long SEED = 15;
		rnd.setSeed(SEED); // 设置种子
		// 因为设置了种子，所以以下代码每回运行结果都一样
		for (int i = 0; i < NUM; i++) {
			System.out.print(rnd.nextInt() + " ");
		}
		System.out.println();
		int x1 = rnd.nextInt(10); // 返回[0,10)之间的随机整数
		int x2 = 3 + rnd.nextInt(15); // 返回[3,17]之间的随机整数
		// 返回a到z之间的随机字符
		char x3 = (char) ('a' + rnd.nextInt('z' - 'a' + 1));
		double x4 = rnd.nextDouble(); // 返回[0.0,1.0)之间的随机数
		System.out.println(x1 + " " + x2 + " " + x3 + " " + x4);
		byte[] bytes = new byte[NUM];
		rnd.nextBytes(bytes);
		for (byte b : bytes) {
			System.out.print(b + " ");
		}

		// testMyRandom();

	}

	private static void testMyRandom() {
		// 使用自定义的MyRandom类。
		MyRandom r = new MyRandom();
		for (int i = 0; i < 100; i++) {
			System.out.println(r.nextBoolean());
		}
	}

}
